# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=nghttp3
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.9.0
pkgrel=1
pkgdesc="HTTP/3 library written in C (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
msys2_repository_url='https://github.com/ngtcp2/nghttp3'
url='https://nghttp2.org/nghttp3/'
license=('spdx:MIT')
checkdepends=("${MINGW_PACKAGE_PREFIX}-cunit")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
)
source=("https://github.com/ngtcp2/nghttp3/releases/download/v${pkgver}/nghttp3-${pkgver}.tar.xz"{,.asc}
        "001-fix-pc-prefix.patch"
        "002-nghttp3-pkgconfig-add-cflags-private.patch")
sha256sums=('b1b531cf6ae5c9366abea2d3319e4ac932fd64c072c8aafd69cf6295733ce12a'
            'SKIP'
            '08559edf597300d34b258c5dab6d16237c6493976e7b4d45a818e0619d21665d'
            '68c831018eb79bc3b43a8fb6a7d013a617dbe2ba7d7971b0993b3cf27107e614')
validpgpkeys=('F4F3B91474D1EB29889BD0EF7E8403D5D673C366') # Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>

prepare() {
  cd "${_realname}-${pkgver}"

  patch -Np1 -i "$srcdir/001-fix-pc-prefix.patch"
  patch -Np1 -i "$srcdir/002-nghttp3-pkgconfig-add-cflags-private.patch"
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DENABLE_SHARED_LIB=ON \
      -DENABLE_STATIC_LIB=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DENABLE_LIB_ONLY=ON \
      ../"${_realname}-${pkgver}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
